package fi.foyt.cs.api.events;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.TaskOptions;

import fi.foyt.cs.persistence.domainmodel.user.User;

public class FriendEventsDispatcher extends EventDispatcher {
  
  public synchronized static FriendEventsDispatcher getInstance() {
    if (INSTANCE == null)
      INSTANCE = new FriendEventsDispatcher();
    return INSTANCE;
  }
  
  private static FriendEventsDispatcher INSTANCE;

  public void sendFriendAddedEvent(User user, User friend) {
    getQueue().add(
      TaskOptions.Builder.withUrl("/friendEventsExecutor")
        .param("event", FriendEvent.FriendAdded.name())
        .param("userId", String.valueOf(user.getId().getId()))
        .param("friendId", String.valueOf(friend.getId().getId()))
    );
  }
  
  public void sendFriendRemoved(User user, User friend) {
    getQueue().add(
      TaskOptions.Builder.withUrl("/friendEventsExecutor")
        .param("event", FriendEvent.FriendRemoved.name())
        .param("userId", String.valueOf(user.getId().getId()))
        .param("friendId", String.valueOf(friend.getId().getId()))
    );
  }
  
  public void sendFriendshipAcceptedEvent(User user, User friend) {
    getQueue().add(
      TaskOptions.Builder.withUrl("/friendEventsExecutor")
        .param("event", FriendEvent.FriendshipAccepted.name())
        .param("userId", String.valueOf(user.getId().getId()))
        .param("friendId", String.valueOf(friend.getId().getId()))
    );
  }
  
  public void sendFriendshipRejectedEvent(User user, User friend) {
    getQueue().add(
      TaskOptions.Builder.withUrl("/friendEventsExecutor")
        .param("event", FriendEvent.FriendshipRejected.name())
        .param("userId", String.valueOf(user.getId().getId()))
        .param("friendId", String.valueOf(friend.getId().getId()))
    );
  }
  
  private Queue getQueue() {
    return getQueue("friend-events");
  }
}
